package com.example.customview.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import androidx.annotation.Nullable;

import com.example.customview.R;

import java.util.logging.Logger;

/**
 * 描述
 *
 * @创建时间 2023/4/1 23:28
 */
public class QqStepView extends View {
    private int mOuterColor = Color.RED;

    private int mInnerColor = Color.GREEN;

    private float mBorderWidth = 20;

    private int mStepTextSize;
    private int mStepColor;

    private Paint mOutPaint;

    private Paint mInnerPaint;

    private Paint mStepTextPaint;
    private int mStepMax;

    private int mCurrentStep;


    public QqStepView(Context context) {
       this(context,null);
    }

    public QqStepView(Context context, @Nullable AttributeSet attrs) {
        this(context,attrs,0);
    }

    public QqStepView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        //获取自定义属性
        TypedArray array = context.obtainStyledAttributes(attrs,R.styleable.QQStepView);
        mOuterColor = array.getColor(R.styleable.QQStepView_outerColor,mOuterColor);
        mInnerColor = array.getColor(R.styleable.QQStepView_innerColor,mInnerColor);
        mBorderWidth = array.getDimension(R.styleable.QQStepView_borderWidth,mBorderWidth);
        mStepTextSize = array.getDimensionPixelSize(R.styleable.QQStepView_stepTextSize,mStepTextSize);
        mStepColor = array.getColor(R.styleable.QQStepView_stepTextColor,mStepColor);

        mOutPaint = new Paint();
        mOutPaint.setAntiAlias(true);
        mOutPaint.setStrokeWidth(mBorderWidth);
        mOutPaint.setColor(mOuterColor);
        mOutPaint.setStrokeCap(Paint.Cap.ROUND);
        mOutPaint.setStyle(Paint.Style.STROKE);// 画笔空心

        mInnerPaint = new Paint();
        mInnerPaint.setAntiAlias(true);
        mInnerPaint.setStrokeWidth(mBorderWidth);
        mInnerPaint.setColor(mInnerColor);
        mInnerPaint.setStrokeCap(Paint.Cap.ROUND);
        mInnerPaint.setStyle(Paint.Style.STROKE);// 画笔空心

        mStepTextPaint = new Paint();
        mStepTextPaint.setAntiAlias(true);
        mStepTextPaint.setColor(mStepColor);
        mStepTextPaint.setTextSize(mStepTextSize);
    }

    private void initPaint(Paint paint,int color){
        paint = new Paint();
        paint.setAntiAlias(true);
        paint.setStrokeWidth(mBorderWidth);
        paint.setColor(color);
        paint.setStrokeCap(Paint.Cap.ROUND);
        paint.setStyle(Paint.Style.STROKE);// 画笔空心
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        // 调用者在布局文件中可能  wrap_content
        // 获取模式 AT_MOST  40DP

        // 宽度高度不一致 取最小值，确保是个正方形
        int width = MeasureSpec.getSize(widthMeasureSpec);
        int height = MeasureSpec.getSize(heightMeasureSpec);
        setMeasuredDimension(width>height?height:width,width>height?height:width);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //画圆弧，圆弧结合，
        RectF rectF = new RectF(mBorderWidth/2,mBorderWidth/2
                ,getWidth()-mBorderWidth/2,getHeight()-mBorderWidth/2);
        // 研究研究
        canvas.drawArc(rectF,135,360,false,mOutPaint);

        if(mStepMax == 0) return;
        float sweepAngle = (float)mCurrentStep/ mStepMax;
        canvas.drawArc(rectF,135,sweepAngle*360,false,mInnerPaint);

        //画文本
        String stepText = mCurrentStep+"";
        Rect textBound = new Rect();
        mStepTextPaint.getTextBounds(stepText,0,stepText.length(),textBound);
        int dx = getWidth()/2 - textBound.width()/2;
       //计算基线
        Paint.FontMetrics fontMetrics = mStepTextPaint.getFontMetrics();
        int dy = (int) ((fontMetrics.bottom - fontMetrics.top)/2 - fontMetrics.bottom);
        int baseLine = getHeight()/2 + dy;
        canvas.drawText(stepText,dx,baseLine,mStepTextPaint);
    }

    public void setStepMax(int stepMax){
        this.mStepMax = stepMax;
    }

    //防止多线程操作出问题，使用sync chronized
    public synchronized void setCurrentStep(int currentStep) {
        this.mCurrentStep = currentStep;
        //不断绘制
        invalidate();
    }
}
